// 文件操作工具类
import { logger } from './logger'

export class FileUtils {
  // 新建文件
  static async newFile(): Promise<{ path: string; content: string }> {
    logger.info('FileUtils.newFile called')
    return {
      path: '',
      content: ''
    }
  }

  // 打开文件
  static async openFile(): Promise<{ path: string; content: string } | null> {
    logger.info('FileUtils.openFile called')
    try {
      const result = await (window as any).electronAPI.openFile()
      logger.info('Open file result:', result)
      if (!result.canceled) {
        return {
          path: result.filePath,
          content: result.content
        }
      }
      return null
    } catch (error) {
      logger.error('打开文件失败:', error)
      return null
    }
  }

  // 保存文件
  static async saveFile(content: string, filePath: string): Promise<boolean> {
    logger.info('FileUtils.saveFile called', { content, filePath })
    try {
      if (filePath) {
        const result = await (window as any).electronAPI.saveFile(content, filePath)
        logger.info('Save file result:', result)
        return result.success
      } else {
        return await this.saveFileAs(content)
      }
    } catch (error) {
      logger.error('保存文件失败:', error)
      return false
    }
  }

  // 另存为
  static async saveFileAs(content: string): Promise<boolean> {
    logger.info('FileUtils.saveFileAs called', { content })
    try {
      const result = await (window as any).electronAPI.saveFileAs(content)
      logger.info('Save file as result:', result)
      return !result.canceled
    } catch (error) {
      logger.error('另存为文件失败:', error)
      return false
    }
  }
}